package com.wuming.caiwu.controller;

import com.wuming.caiwu.core.BusinessException;
import com.wuming.caiwu.core.CommonResult;
import com.wuming.caiwu.model.DmsOrder;
import com.wuming.caiwu.service.DmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * Created by hanfei on 16/4/19.
 */
@Controller
@RequestMapping("/web/order")
public class DmsOrderController extends BaseController {

    @Autowired
    private DmsOrderService orderService;

    @RequestMapping(value = "/manageOrder", method = RequestMethod.GET)
    public String manageOrder(@RequestParam(defaultValue = "0") Integer payType, Model model) {
        model.addAttribute("payType",payType);
        return "/order/manageOrder";
    }

    @RequestMapping(value = "/orderProfitsharing/{storeId}", method = RequestMethod.GET)
    public String orderProfitsharing(@PathVariable Long storeId, Model model) {
        model.addAttribute("payType",0);
        model.addAttribute("storeId",storeId);
        return "/order/orderProfitsharing";
    }

    @ResponseBody
    @RequestMapping(value = "/getProfitsharingMoney", method = RequestMethod.GET)
    public CommonResult getProfitsharingMoney(@RequestParam(required = false) Long storeId,
                                              @RequestParam(required = false) String startDate,
                                              @RequestParam(required = false) String endDate,
                                              @RequestParam(required = false) String orderSn){
        try {
            BigDecimal money = orderService.getProfitsharingMoney(startDate,endDate,orderSn,storeId);
            if (null == money){
                money = BigDecimal.ZERO;
            }
            return CommonResult.createSuccessResult(money.toString());
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        }
    }


    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult list(@RequestParam(required = false) Long storeId,
                             @RequestParam(required = false) String orderSn,
                             @RequestParam(required = false) Integer orderStatus,
                             @RequestParam(required = false) String startDate,
                             @RequestParam(required = false) String endDate,
                             @RequestParam(defaultValue = "0") Integer payType,
                             @RequestParam(defaultValue = "0") int pageIndex, @RequestParam(defaultValue = "10") int pageSize){
        try {
            List<DmsOrder> orders = orderService.getDmsOrderByPage(storeId,orderSn,orderStatus,startDate,endDate,payType,pageIndex, pageSize);
            int total = orderService.getTotal(storeId,orderSn,orderStatus,startDate,endDate,payType);
            return CommonResult.createSuccessResult(orders, total, "查询订单信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("查询订单信息失败.");
        }
    }

    @ResponseBody
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public CommonResult delete(@PathVariable Long id) {
        logger.info("准备删除订单信息ID:{}", id);
        try {
            orderService.removeOrderById(id);
            return CommonResult.createSuccessResult("删除订单信息成功.");
        } catch (BusinessException be) {
            logger.error("发生业务异常,异常信息:{}", be.getMessage());
            return CommonResult.createFailureResult("删除订单信息失败.");
        } catch (Exception e) {
            logger.error("发生系统异常,异常信息:{}", e.getMessage());
            return CommonResult.createFailureResult("删除订单信息失败.");
        }
    }

}
